package com.yanxiu.common;


	import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

	import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.log4testng.Logger;

	public class HttpHelper {
		private static Logger log = Logger.getLogger(HttpHelper.class);
//		private static CloseableHttpClient httpclient = null;
//		private HttpPost post = null;
//		private HttpGet get = null;

//		public static CloseableHttpClient getInstance(){
//			if(httpclient==null){
//				httpclient = HttpClients.createDefault();
//			}
//			return httpclient;
//		}
		public static ResponseResult doGet(String url) throws ClientProtocolException, IOException{
			List<BasicNameValuePair> headers = new ArrayList<>();
			return doGet(url,headers);
		}
		public static ResponseResult doGet(String url,List<BasicNameValuePair> headers) throws ClientProtocolException, IOException{
			HttpGet get = new HttpGet(url);
			ResponseResult responseResult = new ResponseResult();
			for(BasicNameValuePair header:headers){
				get.setHeader(header.getName(), header.getValue());
			}
			CloseableHttpClient client = HttpClients.createDefault();
//			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(2000).build();
//			get.setConfig(requestConfig);
//			
			CloseableHttpResponse response = client.execute(get);
			JSONObject result = null;
			responseResult.setStatus_code(response.getStatusLine().getStatusCode());
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				String ret = EntityUtils.toString(response.getEntity());
				
				try {
					result = new JSONObject(ret);
					responseResult.setBody(result);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				response.close();
				client.close();
				
			}

			
					
			return responseResult;
		}
		public static ResponseResult simpleGet(String url) throws ClientProtocolException, IOException{
			
			log.info("send a simple get request and get the status code");
			
			HttpGet get = new HttpGet(url);
			ResponseResult responseResult = new ResponseResult();
			CloseableHttpClient client = HttpClients.createDefault();
			CloseableHttpResponse response = client.execute(get);
			
			responseResult.setStatus_code(response.getStatusLine().getStatusCode());
			log.info(url+":status code is:"+responseResult.getStatus_code());
			return responseResult;
		}

		public static ResponseResult doPost(String url,List<BasicNameValuePair> body) throws ClientProtocolException, IOException{
			List<BasicNameValuePair> headers = new ArrayList<>();
			return doPost(url,headers,body);
		}	
		public static ResponseResult doPost(String url,List<BasicNameValuePair> headers,List<BasicNameValuePair> body) throws ClientProtocolException, IOException{
			HttpPost post = new HttpPost(url);
			ResponseResult responseResult = new ResponseResult();
			for(BasicNameValuePair header:headers){
				post.setHeader(header.getName(), header.getValue());
			}
			CloseableHttpClient client = HttpClients.createDefault();
			post.setEntity(new UrlEncodedFormEntity(body));
			
			CloseableHttpResponse response = client.execute(post);
			JSONObject result = null;
			responseResult.setStatus_code(response.getStatusLine().getStatusCode());
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				String ret = EntityUtils.toString(response.getEntity());
				
				try {
					result = new JSONObject(ret);
					responseResult.setBody(result);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				response.close();
				client.close();
				
			}

			
					
			return responseResult;
			
		}
	}

